from django.db import models
from front.models import User, UserExtension


class Category(models.Model):
    """文章分类模型"""
    name = models.CharField(max_length=100, null=False)
    create_time = models.DateTimeField(auto_now_add=True)


class Article(models.Model):
    """文章模型"""
    title = models.CharField(max_length=100, null=False)
    content = models.TextField(null=False)
    create_time = models.DateTimeField(auto_now_add=True)

    category = models.ForeignKey('Category', on_delete=models.CASCADE, null=False, related_name='articles')
    user = models.ForeignKey('front.User', on_delete=models.CASCADE, null=False, related_name='articles')

    def __str__(self):
        return '<Article(id=%s, title=%s, content=%s, category=%s, user_id=%s)>' % (
        self.id, self.title, self.content, self.category_id, self.user_id)


class Tag(models.Model):
    name = models.CharField(max_length=100, null=False)
    create_time = models.DateTimeField(auto_now_add=True)

    articles = models.ManyToManyField('Article', related_name='tag')

    def __str__(self):
        return '<Tag(name=%s)>' % self.name
